$(function() {
    $(".header-center-box>input").focus(function() {
        $(".header").addClass("active");
    });

    $(".header-cancel").click(function (){
        $(".header").removeClass("active");
    });
    $(".header-switch>span").click(function() {
        //console.log(this.offsetLeft);
        $(this).addClass("active").siblings().removeClass("active");
        $(".header-switch>i").animate({left: this.offsetLeft}, 100);
    });

    let pageArray = ["home", "video", "me", "friend", "account"];
    $(".footer>ul>li").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        let url = $(this).find("img").attr("src");
        //console.log(url);
        url = url.replace("normal", "selected");
        $(this).find("img").attr("src", url);
        $(this).siblings().find("img").forEach(oImg => {
            oImg.src = oImg.src.replace("selected", "normal");
        });
        
        //console.log($(this).index());
        let currentName = pageArray[$(this).index()];
        $(".header").removeClass().addClass("header " + currentName);
        
    });

    //获取svg path长度
    let svgPathLength = $("#refreshLogo")[0].getTotalLength();
    //初始情况先隐藏
    $("#refreshLogo").css({"stroke-dasharray" : svgPathLength, "stroke-dashoffset" : svgPathLength});


    var myScroll = new IScroll('.main', {
        mouseWheel: false,
        scrollbars: false,
        probeType: 3
    });

    /**

     
    let isPullDown = false;
    let isPullUp = false;
    let isRefresh = false;
    let navHeight = $(".pull-down").height();
    let toolbarHeight = $(".pull-up").height();
    let maxOffsetY = myScroll.maxScrollY - toolbarHeight;
    myScroll.on("scroll", function () {
        // console.log(this.y);
        // console.log(this.maxScrollY);
        if (this.y >= navHeight) {
            let offset = (this.y - navHeight)*3;
            if (svgPathLength >= offset) {
                $("#refreshLogo").css({"stroke-dashoffset" : svgPathLength - offset});
            }else {
                this.minScrollY = 170;
                isPullDown = true;
            }
        }else if(this.y < maxOffsetY) {
            //console.log("底部开始出现");
            this.maxScrollY = maxOffsetY;
            isPullUp = true;
            $(".pull-up>p>span").html("松开加载更多");
            
        }
    });
    myScroll.on("scrollEnd", function () {
        if (isPullDown && !isRefresh) {
            isRefresh = true;
            loadData();
        }else if (isPullUp && !isRefresh) {
            $(".pull-up>p>span").html("加载中...");
            isRefresh = true;
            loadMore();
        }
    });

    function loadData() {
        console.log("加载数据");
        setTimeout(() => {
            isPullDown = false;
            isRefresh = false;
            myScroll.minScrollY = 0;
            myScroll.scrollTo(0, 0);
            $("#refreshLogo").css({"stroke-dashoffset" : svgPathLength});
            console.log("加载数据完成");
        }, 3000);
    }
    function loadMore() {
        console.log("加载数据");
        setTimeout(() => {
            isPullUp = false;
            isRefresh = false;
            myScroll.maxOffsetY = maxOffsetY + toolbarHeight;
            myScroll.scrollTo(0, myScroll.maxOffsetY);
            console.log("加载数据完成");
        }, 3000);
    }
    */

    /* 创建轮播*/
    var mySwiper = new Swiper ('.swiper-container', {
        autoplay: {
            delay: 2000,//1秒切换一次
        },
        loop: true, // 循环模式选项
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
          bulletClass: 'my-bullet',//需设置.my-bullet样式
          bulletActiveClass: 'my-bullet-active',
        },
        observer: true,
        observeParents: true,
        observeSlideChildren: true,
    });

    HomeApis.getHomeBanner()
    .then(function (data) {
        console.log(data);
        
        var bannerHtml = template('bannerSlide', data);
        $(".swiper-wrapper").html(bannerHtml);
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
        
    });

    $(".nav i").html(new Date().getDate());

    //推荐歌单
    HomeApis.getPersonRecommends()
    .then(function (data) {
        var recomendHtml = template('personRecommend', data);
        $(".recommend-bottom").html(recomendHtml);
        //限制2行
        $(".recommend-title").forEach(function (ele) {
            $clamp(ele, {clamp: 2});
        });
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
    });

    //独家放送
    HomeApis.getHomeExclusive()
    .then(function (data) {
        let exclusiveHtml = template('homeExclusive', data);
        $(".exclusive-bottom").html(exclusiveHtml);
        $(".exclusive-title").forEach(function (ele) {
            $clamp(ele, {clamp: 2});
        });
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
        
    });
    
    //新碟新歌
    HomeApis.getHomeAlbum()
    .then(function (data) {
        let albumHtml = template('newAlbum', data);
        $(".album-bottom").html(albumHtml);
        $(".album-title").forEach(function (ele) {
            $clamp(ele, {clamp: 1});
        });
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
        
    });

    //推荐MV
    HomeApis.getHomeMV()
    .then(function (data) {
        let mvHtml = template('personMV', data);
        $(".mv-bottom").html(mvHtml);
        $(".mv-title").forEach(function (ele) {
            $clamp(ele, {clamp: 1});
        });
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
        
    });

    //推荐主播
    HomeApis.getHomeDJ()
    .then(function (data) {
        let djHtml = template('personDJ', data);
        $(".dj-bottom").html(djHtml);
        $(".dj-title").forEach(function (ele) {
            $clamp(ele, {clamp: 2});
        });
        myScroll.refresh();
    })
    .catch(function (error) {
        console.log(error);
        
    });

});